from django.shortcuts import render
from .models import BookInfo, HeroInfo
import logging
from datetime import date
from django.db.models import *

"""
基本查询
"""
# Create your views here.
# """新增"""
# book = BookInfo(
#     btitle='dada',
#     bpub_date=date(1988, 1, 1),
#     bread=10,
#     bcomment=10)
# book.save()
# hero = HeroInfo(
#     hname='孙悟空',
#     hgender=0,
#     hbook=book
#     hbook_id= book.id
# )
# hero.save()

# HeroInfo.objects.create(
#     hname='沙悟净',
#     hgender=0,
#     hbook=book
# )

# hero = BookInfo.objects.get(id=16)
# hero.delete()

# BookInfo.objects.filter(id=14).delete()

# hero = HeroInfo.objects.get(hname='袁紫衣')
# hero.hname = '猪悟能'
# hero.save()

# HeroInfo.objects.filter(hname='沙悟净').update(hname='沙僧')

# a=BookInfo.objects.all()
# logging.error(a)

# b= BookInfo.objects.get(id=6)
# logging.error(b)


# c=BookInfo.objects.get(btitle="西游记")
# print(c.id)

# e = BookInfo.objects.count()
# print(e)

# f = BookInfo.objects.filter(id__exact=6)
# print(f)

# g =BookInfo.objects.filter(btitle__contains = '传')
# print(g)

# h = BookInfo.objects.filter(btitle__endswith = "游")
# print(h)

# q = BookInfo.objects.filter(btitle__startswith= '西')
# print(q)

# w = BookInfo.objects.filter(btitle__isnull=False)
# print(w)

# o = BookInfo.objects.filter(id__in=[1,3,6])
# print(o)

# t= BookInfo.objects.filter(id__gt=2)
# print(t)

# t= BookInfo.objects.filter(id__lt=2)
# print(t)

# t= BookInfo.objects.filter(id__gte=2)
# print(t)

# y =BookInfo.objects.exclude(id=2)
# print(y)
# print(BookInfo.objects.filter(bpub_date__year=1980))
# print(BookInfo.objects.filter(bpub_date__year__month__day=date(1980, 5, 1)))
# 查询阅读量大于等于评论量的图书。
# print(BookInfo.objects.filter(bread__gt=F('bcomment')))
# print(BookInfo.objects.filter(bread__gt=F('bcomment')*2))

# print(BookInfo.objects.filter(Q(bread__gt=20)))

# 查询阅读量大于20，或编号小于3的图书，只能使用Q对象实现
# print(BookInfo.objects.filter(Q(bread__gt=20) & Q(pk__lt=3)))
# print(BookInfo.objects.filter(Q(bread__gt=20) | Q(pk__lt=3)))
# print(BookInfo.objects.filter(~Q(bread__gt=20)))

# print(BookInfo.objects.aggregate(Sum('bread')))
# print(BookInfo.objects.aggregate(Max('bread')))
# print(BookInfo.objects.aggregate(Min('bread')))
# print(BookInfo.objects.count())
# print(BookInfo.objects.Max('bread'))
# 升序
# print(BookInfo.objects.order_by('bread'))
# print(BookInfo.objects.order_by('-bread'))
# b = BookInfo.objects.get(id=1)
# print(b.heroinfo_set.all())

# b = HeroInfo.objects.get(hname="郭靖")
# p=b.hbook
# print(p)


# print(BookInfo.objects.filter(heroinfo__hname='王语嫣'))

# print(BookInfo.objects.filter(heroinfo__hcomment__contains='八'))

print(HeroInfo.objects.filter(hbook__bread__gt=30))

BookInfo.objects.fileter(bread__gt=30).der_by('bpub_date')
# todo 这里没做完
# print("asas")
